package com.kameiqi.erp.config;

import cn.qqxkj.core.agile.controller.AgilePageApiImpl;
import cn.qqxkj.core.permission.config.PermissionInterceptor;
import cn.qqxkj.core.permission.controller.PermissionApiImpl;
import cn.qqxkj.core.permission.vo.PermissionPackageVo;
import com.kameiqi.erp.constant.PermissionMenus;
import com.kameiqi.erp.controller.ErpHomeController;
import com.kameiqi.erp.service.redis.RedisService;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 小六子
 */
@Configuration
public class PermissionConfig extends PermissionInterceptor {
    @Resource
    private RedisService redisService;

    @Override
    public List<PermissionPackageVo> addPermissionPackages() {
        List<PermissionPackageVo> packages = new ArrayList<>();
        packages.add(PermissionPackageVo.builder()
                .classPackage(ErpHomeController.class)
                .name("进销存管理").pos(999).uri(PermissionMenus.ERP).parentUri("/").icon("profile")
                .menuItem(true)
                .build());
        packages.add(PermissionPackageVo.builder()
                .name("产品管理").pos(999).uri(PermissionMenus.MATERIAL).parentUri(PermissionMenus.ERP).icon("profile")
                .menuItem(true)
                .build());
        packages.add(PermissionPackageVo.builder()
                .name("系统管理").pos(0).uri("/core").parentUri("/").icon("setting")
                .menuItem(true)
                .build());
        packages.add(PermissionPackageVo.builder()
                .name("开发配置").pos(66).uri("/agile").parentUri("/core").icon("setting")
                .menuItem(true)
                .build());
        //分析 classes 下的类权限
        packages.add(PermissionPackageVo.builder()
                .classPackage(PermissionApiImpl.class)
                .name("权限管理").uri("/permission").pos(0).parentUri("/core").icon("setting")
                .menuItem(true)
                .build());
        packages.add(PermissionPackageVo.builder()
                .name("公共参数").pos(66).uri("/common").parentUri("/core").icon("setting")
                .menuItem(true)
                .build());
        return packages;
    }

    @Override
    protected boolean handlePermissionCheck(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return true;
//        // 判断是否不需要权限检查
//        String requestUrl = request.getRequestURI();
//        String url = requestUrl.replaceFirst(Pattern.quote(request.getContextPath()),"");
//        if (url.startsWith("/doc.html") || url.startsWith("/validateCode") ||
//                url.startsWith("/agileTemplate/") || url.startsWith("/agile/page/") ||
//                url.startsWith("/permission/login") || url.startsWith("/admin/login") ||
//                url.startsWith("/user/login") || url.startsWith("/user/register")) {
//            return true;
//        }
//        // 判断是否管理员
//        final Admin o = PermissionUtil.getLoginUser();
//        if(o != null){
//            return super.handlePermissionCheck(request, response);
//        }else{
//            //具体，比如：处理若用户未登录，则跳转到登录页
//            Object userId = redisService.getObjectFromSessionByKey(request,"userId");
//            if(userId!=null) { //如果已登录，不阻止
//                return true;
//            }else{
//                return false;
//            }
//        }
    }

    @Override
    public List<String> addPermissionUncheckUrls() {
        return null;
    }
}
